import { type BToastProps, useToast } from 'bootstrap-vue-next';
<%_ if (enableTranslation) { _%>
import { type Composer, useI18n } from 'vue-i18n';
<%_ } _%>

export const useAlertService = () => {
  const toast = useToast();
  if (!toast) {
    throw new Error('BootstrapVue toast component was not found');
  }
<%_ if (enableTranslation) { _%>
  const i18n = useI18n();
<%_ } _%>
  return new AlertService({
    toast,
<%_ if (enableTranslation) { _%>
    i18n,
<%_ } _%>
  });
};

export default class AlertService {
  private toast: ReturnType<typeof useToast>;
<%_ if (enableTranslation) { _%>
  private i18n: Composer;
<%_ } _%>

  constructor({
    toast,
<%_ if (enableTranslation) { _%>
    i18n,
<%_ } _%>
  }: {
    toast: ReturnType<typeof useToast>;
<%_ if (enableTranslation) { _%>
    i18n: Composer,
<%_ } _%>
  }) {
    this.toast = toast;
<%_ if (enableTranslation) { _%>
    this.i18n = i18n;
<%_ } _%>
  }

  showInfo(toastMessage: string, props: BToastProps = {}) {
    this.toast.show!({
      props: {
        pos: 'top-center',
        title: 'Info',
        variant: 'info',
        solid: true,
        body: toastMessage,
        ...props,
      },
    });
  }

  showSuccess(toastMessage: string) {
    this.showInfo(toastMessage, {
      title: 'Success',
      variant: 'success',
    });
  }

  showError(toastMessage: string) {
    this.showInfo(toastMessage, {
      title: 'Error',
      variant: 'danger',
    });
  }

  showHttpError(httpErrorResponse: any) {
    let errorMessage: string | null = null;
    switch (httpErrorResponse.status) {
      case 0:
        errorMessage = this.i18n.t('error.server.not.reachable').toString();
        break;

      case 400: {
        const arr = Object.keys(httpErrorResponse.headers);
<%_ if (enableTranslation) { _%>
        let entityKey: string | null = null;
<%_ } _%>
        for (const entry of arr) {
          if (entry.toLowerCase().endsWith('app-error')) {
            errorMessage = httpErrorResponse.headers[entry];
<%_ if (enableTranslation) { _%>
          } else if (entry.toLowerCase().endsWith('app-params')) {
            entityKey = httpErrorResponse.headers[entry];
<%_ } _%>
          }
        }
<%_ if (enableTranslation) { _%>
        if (errorMessage && entityKey) {
          errorMessage = this.i18n.t(errorMessage, { entityName: this.i18n.t(`global.menu.entities.${entityKey}`) }).toString();
        } else if (!errorMessage) {
          errorMessage = this.i18n.t(httpErrorResponse.data.message).toString();
        }
<%_ } else { _%>
        if (!errorMessage && httpErrorResponse.data?.fieldErrors) {
          errorMessage = 'Validation error';
        } else if (!errorMessage) {
          errorMessage = httpErrorResponse.data.message;
        }
<%_ } _%>
        break;
      }

      case 404:
        errorMessage = this.i18n.t('error.http.404').toString();
        break;

      default:
<%_ if (enableTranslation) { _%>
        errorMessage = this.i18n.t(httpErrorResponse.data.message).toString();
<%_ } else { _%>
        errorMessage = httpErrorResponse.data.message;
<%_ } _%>
    }
    this.showError(errorMessage);
  }
}
